package com.wx.demo;

import com.sun.tools.attach.AgentInitializationException;
import com.sun.tools.attach.AgentLoadException;
import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Objects;
import java.util.Scanner;

public class AttachMain {
    public static void main(String[] args) throws IOException, AttachNotSupportedException, AgentLoadException, AgentInitializationException {
        Process jps = Runtime.getRuntime().exec("jps");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(jps.getInputStream()));
        try {
            String line;
            while (Objects.nonNull(line = bufferedReader.readLine())) {
                System.out.println(line);
            }
        } finally {
            bufferedReader.close();
        }
        Scanner scanner = new Scanner(System.in);
        String processId = scanner.next();
        VirtualMachine vm = VirtualMachine.attach(processId);
        vm.loadAgent("E:\\javaagentutil\\target\\agent-agent-1.0-SNAPSHOT-jar-with-dependencies.jar");
    }
}
